package com.isometricProjectionExmaple
{
	import com.AStarExample.AStar;
	import com.AStarExample.Grid;
	import com.AStarExample.Node;
	
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.filters.GlowFilter;
	import flash.geom.Point;
	
	public class TileTest extends Sprite
	{
		private var objListArr:Array;
		private var wo:DrawnIsoTile;
		private var grid:Grid;
		private var aStar:AStar;
		private var pathArr:Array;
		private var tipSp:DrawnIsoTile;
		public function TileTest()
		{
			super();
			init();
		}
		private var world:Sprite;
		private var objSp:Sprite;
		private function init():void
		{
		
			objListArr = [];
			objSp = new Sprite();
			world = new Sprite();
			world.x=objSp.x =800;
			world.y=objSp.y = 100;
			this.addChild(world);
			this.addChild(objSp);
			this.creatGrid();
			for(var i:int = 0;i<30;i++)
			{
				for(var j:int = 0;j<30;j++)
				{
					var node:Node = this.grid.getNode(i,j);
					var color:uint = this.getColor(node);
					var tile:DrawnIsoTile = new DrawnIsoTile(20,color);
					tile.position = new Point3D(node.x*20,0,node.y*20);
					tile.addEventListener(MouseEvent.ROLL_OVER,haha);
					
					world.addChild(tile);
					//objListArr.push(tile)
				}
			}
			sortList();
			
			tipSp = new DrawnIsoTile(20,0x00ff00);
			this.world.addChild(tipSp);
			tipSp.filters = [new GlowFilter(0x00ff00,1,3,3,4,3)];
			world.addEventListener(MouseEvent.CLICK,findPathHandler); 
			
		} 
		
		private function creatGrid():void
		{
			this.grid = new Grid(30,30);
			this.grid.setStartNode(0,0);
			for(var i:int = 0;i<200;i++)
			{
				var $x:int = Math.floor(Math.random()*30);
				var $y:int = Math.floor(Math.random()*30);
				if(!this.grid.getNode($x,$y).walkable)continue;
				if($x!=0 || $y!=0)this.grid.getNode($x,$y).walkable = false;
				wo = new DrawnIsoBox(20,Math.random()*0xffffff,20);
				this.objSp.addChild(wo);
				wo.position = new Point3D($x*20,0,$y*20);
				this.objListArr.push(wo);
			}
			
			wo = new DrawnIsoBox(20,Math.random()*0xffffff,60);
			this.objSp.mouseEnabled = false;
			this.objSp.mouseChildren = false;
			this.objSp.addChild(wo);
			this.objListArr.push(wo);
			this.sortList();
		}
		
		private function haha(e:MouseEvent):void
		{
			tipSp.position = DrawnIsoTile(e.currentTarget).position
		}
		
		
		
		private function onClickAddBoxToStage(e:MouseEvent):void
		{
			
		}
		
		private function findPathHandler(e:MouseEvent):void
		{
			var point3D:Point3D = IsoUtils.screenToIso(new Point(this.world.mouseX,this.world.mouseY));
			var $x:int = Math.round(point3D.x/20);
			var $y:int = Math.round(point3D.z/20);
			this.grid.setEndNode($x,$y);
			 $x = Math.round(this.wo.x/20);
			 $y = Math.round(this.wo.z/20);
			this.grid.setStartNode($x,$y);
			this.aStar = new AStar();
			var boo:Boolean = this.aStar.findPath(this.grid);
			if(boo)
			{
				this.pathArr = this.aStar.getPath();
				if(!this.pathArr ||this.pathArr.length==0)return;
				if(!this.hasEventListener(Event.ENTER_FRAME))this.addEventListener(Event.ENTER_FRAME,haha4);
			}
			
		}
		
		private function haha4(e:Event):void
		{
			var node :Node = this.pathArr[0];
			if(Point.distance(new Point(node.x*20,node.y*20),new Point(this.wo.x,this.wo.z))<1)
			{
				var node2:Node = this.pathArr.shift();
				if(node2)this.grid.setEndNode(node2.x,node2.y);
				if(this.pathArr.length == 0)
				{
					this.removeEventListener(Event.ENTER_FRAME,haha4);
					return;
				}
				node= this.pathArr[0];
			}
			
			var vx:Number = 0;
			var vz:Number = 0;
			if(node.x*20>this.wo.x)
			{
				vx = 4;
			}else if(node.x*20<this.wo.x)
			{
				vx = -4
			}
			
			if(node.y*20>this.wo.z)
			{
				vz = 4;
			}else if(node.y*20<this.wo.z)
			{
				vz = -4
			}
			
			this.wo.x +=vx;
			this.wo.z+=vz;
			this.sortList();
		}
		
		private function sortList():void
		{
			objListArr.sortOn("depth",Array.NUMERIC);
			for(var i:int = 0;i<this.objListArr.length;i++)
			{
				this.objSp.setChildIndex(objListArr[i],i);
			}
		}
		
		/**根据节点返回颜色**/
		private function getColor(node:Node):uint
		{
			if(!node.walkable)return 0;
			if(node == this.grid.startNode )return 0xff0000;
			if(node == this.grid.endNode)
			{
				return 0x00ff00;
			}
			return 0xffffff;
		}
	}
}